package life.phone.upad

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast

class ScreenBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val strAction = intent.action
        when (strAction) {
            Intent.ACTION_SCREEN_OFF -> {
                //屏幕锁屏
                Log.i("screenBR", "屏幕锁屏：ACTION_SCREEN_OFF触发")
                // val keyguardManager = context.getSystemService(Activity.KEYGUARD_SERVICE) as KeyguardManager
                // val keyguardLock = keyguardManager.newKeyguardLock("")
                // keyguardLock.disableKeyguard()//解锁系统锁屏
                val mLockIntent = Intent(context, MainActivity::class.java)
                mLockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
                context.startActivity(mLockIntent)//跳转到主界面
            }
            Intent.ACTION_SCREEN_ON -> {
                //屏幕解锁(实际测试效果，不能用这个来判断解锁屏幕事件)
                //【因为这个是解锁的时候触发，而解锁的时候广播还未注册】
                Log.i("screenBR", "屏幕解锁：ACTION_SCREEN_ON触发")
                Toast.makeText(context, "解锁了", Toast.LENGTH_SHORT).show()
            }
            Intent.ACTION_USER_PRESENT -> {
                //屏幕解锁(该Action可以通过静态注册的方法注册)
                //在解锁之后触发的，广播已注册
                Log.i("screenBR", "屏幕解锁：ACTION_USER_PRESENT触发")
                Toast.makeText(context, "解锁了", Toast.LENGTH_SHORT).show()
            }
            else -> {
                //nothing
            }
        }
    }
}